package com.changli03.threadsafe.exer;

public class Account {
    public static void main(String[] args) {
        account1 acc1 = new account1();
        Thread t1 = new Thread(acc1);
        Thread t2 = new Thread(acc1);
        t1.setName("储户1");
        t2.setName("储户2");
        t1.start();
        t2.start();
    }
}
class account1 implements Runnable{
   int balance;
    @Override
    public void run() {
//        synchronized (this){
            deposit(1000);

//        }
    }
    public synchronized void deposit(int amount) {
        for (int i = 0; i <3 ; i++) {
            if ( amount > 0 ) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                balance = balance + amount;
                System.out.println(Thread.currentThread().getName()+"余额:"+ balance);
            }
        }
    }
}
